package com.ytd.smartcanteen.weighting.adapter;

import android.annotation.SuppressLint;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.ytd.common.base.BaseApplication;
import com.ytd.common.bean.BaseRequest;
import com.ytd.common.net.NetCallBack;
import com.ytd.common.net.RequestManager;
import com.ytd.common.util.LogUtil;
import com.ytd.smartcanteen.weighting.MainActivity;
import com.ytd.smartcanteen.weighting.bean.MealInfo;
import com.ytd.smartcanteen.weighting.util.FoodManager;
import com.ytd.smartcanteen.weighting.util.RequestUtil;

import java.util.ArrayList;
import java.util.List;

import cn.ytd.smartcanteen.weighting.R;

public class DishRecyclerAdapter extends RecyclerView.Adapter<DishRecyclerAdapter.RecyclerViewHolder> {
    private final MainActivity mContext;
    private String mealTimeId, foodCategoryId;

    private List<MealInfo> mDataList = new ArrayList<>();

    private ExpandableAdapter mExpandableAdapter;

    public void setExpandableAdapter(ExpandableAdapter expandableAdapter) {
        mExpandableAdapter = expandableAdapter;
    }

    public DishRecyclerAdapter(MainActivity context) {
        mContext = context;
    }

    @SuppressLint("NotifyDataSetChanged")
    public void setDataList(final List<MealInfo> mDataList, String mealID, String foodCategID) {
        this.mDataList = mDataList;
        mealTimeId = mealID;
        foodCategoryId = foodCategID;
        notifyDataSetChanged();
    }

    @NonNull
    @Override
    public RecyclerViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.goods_list_item, parent, false);
        return new RecyclerViewHolder(view);
    }

    @Override
    public void onBindViewHolder(RecyclerViewHolder recyclerViewHolder, int position) {
        MealInfo mListBean = mDataList.get(position);
        recyclerViewHolder.mTitle.setText(mListBean.foodName);
        // 价格 ¥2.68/50g
        String price = "";
        switch (mListBean.calcType) {//计量类型（按重量0、按个数1、统一价格2）
            default:
            case 0:
                price = mContext.getString(R.string.text_price) + " ¥" + mListBean.price + "/" + mListBean.weight +
                        mListBean.weightUnit + " ";
                break;
            case 1:
                price =
                        mContext.getString(R.string.text_price) + " ¥" + mListBean.price + "/" + mListBean.numberUnitName;
                break;
            case 2:
                price = mContext.getString(R.string.text_price) + " ¥" + mListBean.price;
                break;
        }
        recyclerViewHolder.mPrice.setText(price);
        FoodManager.getInstance().setFoodBound(mealTimeId, mListBean);
        recyclerViewHolder.mState.setText(
                mListBean.bound ? mContext.getString(R.string.text_bound) : mContext.getString(
                        R.string.text_binding));
        recyclerViewHolder.mState.setBackgroundResource(mListBean.bound ?
                R.drawable.bg_pop_ry_state_y : R.drawable.bg_pop_ry_state_n);

        recyclerViewHolder.mState.setOnClickListener(v -> {
            if (!mListBean.bound) {
                bindFood(mListBean);
            }
        });
    }

    @SuppressLint("NotifyDataSetChanged")
    private void bindFood(MealInfo mealInfo) {
        if (BaseApplication.getInstance().isLocalMode()) {
            FoodManager.getInstance().bindFood(mealTimeId, mealInfo);
            notifyDataSetChanged();
            if (mExpandableAdapter != null) {
                mExpandableAdapter.notifyDataSetChanged();
            }
            return;
        }
        BaseRequest request = RequestUtil.updateFoodInfo(mealTimeId, mealInfo);
        RequestManager.getInstance().request(request, new NetCallBack<Object>() {
            @Override
            public void onSuccess(Object result) {
                FoodManager.getInstance().bindFood(mealTimeId, mealInfo);
                notifyDataSetChanged();
                if (mExpandableAdapter != null) {
                    mExpandableAdapter.notifyDataSetChanged();
                }
            }

            @Override
            public void onFail(int code, String msg) {
                LogUtil.d("updateFoodInfo onFail " + code + "--" + msg);
            }
        });
    }

    @Override
    public int getItemCount() {
        return mDataList == null ? 0 : mDataList.size();
    }

    public static class RecyclerViewHolder extends RecyclerView.ViewHolder {
        private final TextView mTitle;
        private final TextView mPrice;
        private final TextView mState;

        RecyclerViewHolder(View itemView) {
            super(itemView);
            mTitle = (TextView) itemView.findViewById(R.id.title);
            mPrice = (TextView) itemView.findViewById(R.id.price);
            mState = (TextView) itemView.findViewById(R.id.state);
        }
    }
}
